package com.atguigu.guli.service.edu.controller.admin;

import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFileService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author dyqstart
 * @create 2021-07-02-10:50
 * @since 1.0.0
 */
//@CrossOrigin
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private OssFileService ossFileService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list).code(20000).message("查询列表成功");
    }

    @ApiOperation(value = "根据ID删除讲师",notes = "根据ID删除讲师，逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "讲师ID",required = true) @PathVariable String id){

        //删除头像
        boolean b = teacherService.removeAvatarById(id);
        if (!b){
            log.warn("讲师头像删除失败，讲师id"+id);
        }

        //删除讲师
        boolean result = teacherService.removeById(id);
        if (result){
            return R.ok().data("items",1).message("删除讲师成功");
        }else {
            return R.error().data("items",-1).message("删除讲师失败");
        }
    }

    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码",required = true)@PathVariable Long page,
                      @ApiParam(value = "每页记录数",required = true)@PathVariable Long limit,
                      @ApiParam(value = "讲师列表查询对象")TeacherQuery teacherQuery){
        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageModel",pageModel);
    }

    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(@ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }


    @ApiOperation("更新讲师")
    @PutMapping("update")
    public R update(@ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新出错");
        }
    }

    @ApiOperation("查询讲师")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "查询讲师的id",required = true) @PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        if (teacher==null){
            return R.error().message("查无此人");
        }else {
            return R.ok().message("查询成功").data("item",teacher);
        }
    }

    @ApiOperation("根据id列表批量删除讲师")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "讲师id列表",required = true)
            @RequestBody List<String> idList){
        boolean result = teacherService.removeByIds(idList);
        if (result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("根据左关键字查询讲师列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @ApiParam(value = "查询关键字",required = true)
            @PathVariable String key){
        List<Map<String,Object>> nameList = teacherService.selectNameListByKey(key);
        return R.ok().data("nameList", nameList);
    }

    @ApiOperation("测试服务调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        return R.ok();
    }

}